package doc;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import util.Translation;
import util.YouDaoTranslation;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017/5/24 15:34
 */
public class JdkDoc {
    public static void main(String[] args) throws IOException {
        Translation youdao = new YouDaoTranslation();
        List<String> list = read("F:\\源码\\src\\java\\io\\File.java");
        Set<String> str = new HashSet<>();
        String[] strings;
        for (String s : list) {
//            strings = s.split("[^a-zA-Z]{2,}");
            strings = s.split("[^a-zA-Z]+");
            for (String string : strings) {
                if (StringUtils.isNotBlank(string)
                        && string.length() > 1) {
                    str.add(string);
                }
            }
        }
        String translate;
        for (String s : str) {
            try {
                translate = youdao.translation(s);
                if (StringUtils.isNotBlank(translate)) {
                    System.out.println(s + " : " + translate);
                }
            } catch (Exception e) {
                System.out.println(s + " : 请求失败");
            }
        }
    }

    public static JSONArray sbTranslation(String word) throws IOException {
        // 调用接口获取json
        Connection.Response d = Jsoup.connect("https://www.shanbay.com/api/v1/bdc/search/?version=2&word="
                + word
                + "&_=1495615632550").ignoreContentType(true).execute();
        JSONObject obj = JSONObject.fromObject(d.body());
        if (0 == obj.optInt("status_code")) {
            return obj.optJSONObject("data").optJSONObject("definitions").optJSONArray("cn");
        }
        return null;
    }

    /**
     *
     * @param fileName
     */
    public static List<String> read(String fileName) {
        List<String> list = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(
                    //                new FileInputStream("C:\\Users\\ucmed\\Desktop\\20170427access.log.tar.gz"),"GBK"));
                    new FileInputStream(fileName), "GBK"));
            int i = 0;
            StringBuffer sb = new StringBuffer();
            JSONObject obj;
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                list.add(line);
//                System.out.println(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
